/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */
 
using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 按钮视图
    /// </summary>
    public class FCButton : FCView {
        /// <summary>
        /// 创建按钮
        /// </summary>
        public FCButton() {
            m_size = new FCSize(60, 20);
        }

        public String m_disabledBackImage;

        /// <summary>
        /// 获取不可用时的背景图片
        /// </summary>
        public virtual String getDisabledBackImage() {
            return m_disabledBackImage;
        }

        /// <summary>
        /// 设置不可用时的背景图片
        /// </summary>
        public virtual void setDisabledBackImage(String value) {
            m_disabledBackImage = value;
        }

        public String m_hoveredBackImage;

        /// <summary>
        /// 获取触摸悬停时的背景图片
        /// </summary>
        public virtual String getHoveredBackImage() {
            return m_hoveredBackImage;
        }

        /// <summary>
        /// 设置触摸悬停时的背景图片
        /// </summary>
        public virtual void setHoveredBackImage(String value) {
            m_hoveredBackImage = value;
        }

        public String m_pushedBackImage;

        /// <summary>
        /// 获取触摸按下时的背景图片
        /// </summary>
        public virtual String getPushedBackImage() {
            return m_pushedBackImage;
        }

        /// <summary>
        /// 设置触摸按下时的背景图片
        /// </summary>
        public void setPushedBackImage(String value) {
            m_pushedBackImage = value;
        }

        public FCContentAlignment m_textAlign = FCContentAlignment.MiddleCenter;

        /// <summary>
        /// 获取文字的布局方式
        /// </summary>
        public virtual FCContentAlignment getTextAlign() {
            return m_textAlign;
        }

        /// <summary>
        /// 设置文字的布局方式
        /// </summary>
        public virtual void setTextAlign(FCContentAlignment value) {
            m_textAlign = value;
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "Button";
        }

        /// <summary>
        /// 获取要绘制的背景色
        /// </summary>
        /// <returns>背景色</returns>
        public override long getPaintingBackColor() {
            long backColor = base.getPaintingBackColor();
            if (backColor != FCColor.None && isPaintEnabled(this)) {
                FCNative native = getNative();
                if (this == native.getPushedView()) {
                    backColor = FCColor.Pushed;
                } else if (this == native.getHoveredView()) {
                    backColor = FCColor.Hovered;
                }
            }
            return backColor;
        }

        /// <summary>
        /// 获取用于绘制的背景图片
        /// </summary>
        public override String getPaintingBackImage() {
            String backImage = null;
            if (isPaintEnabled(this)) {
                FCNative native = getNative();
                if (this == native.getPushedView()) {
                    backImage = m_pushedBackImage;
                } else if (this == native.getHoveredView()) {
                    backImage = m_hoveredBackImage;
                }
            } else {
                backImage = m_disabledBackImage;
            }
            if (backImage != null) {
                return backImage;
            } else {
                return base.getPaintingBackImage();
            }
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public override void getAttribute(String name, ref String value, ref String type)
        {
            switch (name)
            {
                case "disabledbackimage":
                    type = "text";
                    value = getDisabledBackImage();
                    break;
                case "hoveredbackimage":
                    type = "text";
                    value = getHoveredBackImage();
                    break;
                case "pushedbackimage":
                    type = "text";
                    value = getPushedBackImage();
                    break;
                case "textalign":
                    type = "enum:FCContentAlignment";
                    value = FCTran.contentAlignmentToStr(getTextAlign());
                    break;
                default:
                    base.getAttribute(name, ref value, ref type);
                    break;
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns></returns>
        public override ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.AddRange(new String[] { "DisabledBackImage", "HoveredBackImage", "PushedBackImage", "TextAlign" });
            return attributeNames;
        }

        /// <summary>
        /// 触摸按下方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchDown(FCTouchInfo touchInfo) {
            base.onTouchDown(touchInfo);
            invalidate();
        }

        /// <summary>
        /// 触摸进入方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchEnter(FCTouchInfo touchInfo) {
            base.onTouchEnter(touchInfo);
            invalidate();
        }

        /// <summary>
        /// 触摸离开方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchLeave(FCTouchInfo touchInfo) {
            base.onTouchLeave(touchInfo);
            invalidate();
        }

        /// <summary>
        /// 触摸抬起方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchUp(FCTouchInfo touchInfo) {
            base.onTouchUp(touchInfo);
            invalidate();
        }

        /// <summary>
        /// 重绘前景方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override void onPaintForeground(FCPaint paint, FCRect clipRect) {
            String text = getText();
            //绘制文字
            if (text != null && text.Length > 0) {
                int width = getWidth(), height = getHeight();
                if (width > 0 && height > 0) {
                    FCFont font = getFont();
                    FCSize tSize = paint.textSize(text, font, -1);
                    FCPoint tPoint = new FCPoint((width - tSize.cx) / 2, (height - tSize.cy) / 2);
                    FCPadding padding = getPadding(); ;
                    switch (m_textAlign) {
                        case FCContentAlignment.BottomCenter:
                            tPoint.y = height - tSize.cy;
                            break;
                        case FCContentAlignment.BottomLeft:
                            tPoint.x = padding.left;
                            tPoint.y = height - tSize.cy - padding.bottom;
                            break;
                        case FCContentAlignment.BottomRight:
                            tPoint.x = width - tSize.cx - padding.right;
                            tPoint.y = height - tSize.cy - padding.bottom;
                            break;
                        case FCContentAlignment.MiddleLeft:
                            tPoint.x = padding.left;
                            break;
                        case FCContentAlignment.MiddleRight:
                            tPoint.x = width - tSize.cx - padding.right;
                            break;
                        case FCContentAlignment.TopCenter:
                            tPoint.y = padding.top;
                            break;
                        case FCContentAlignment.TopLeft:
                            tPoint.x = padding.left;
                            tPoint.y = padding.top;
                            break;
                        case FCContentAlignment.TopRight:
                            tPoint.x = width - tSize.cx - padding.right;
                            tPoint.y = padding.top;
                            break;
                    }
                    FCRect tRect = new FCRect(tPoint.x, tPoint.y, tPoint.x + tSize.cx, tPoint.y + tSize.cy);
                    long textColor = getPaintingTextColor();
                    if (autoEllipsis() && (tRect.right > clipRect.right || tRect.bottom > clipRect.bottom)) {
                        if (tRect.right > clipRect.right) {
                            tRect.right = clipRect.right;
                        }
                        if (tRect.bottom > clipRect.bottom) {
                            tRect.bottom = clipRect.bottom;
                        }
                        paint.drawTextAutoEllipsis(text, textColor, font, tRect);
                    } else {
                        paint.drawText(text, textColor, font, tRect, -1);
                    }
                }
            }
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value)
        {
            switch (name)
            {
                case "disabledbackimage":
                    setDisabledBackImage(value);
                    break;
                case "hoveredbackimage":
                    setHoveredBackImage(value);
                    break;
                case "pushedbackimage":
                    setPushedBackImage(value);
                    break;
                case "textalign":
                    setTextAlign(FCTran.strToContentAlignment(value));
                    break;
                default:
                    base.setAttribute(name, value);
                    break;
            }
        }
    }
}
